﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 护士工作站
{
    public partial class 新增医嘱处理 : Form
    {
        public 新增医嘱处理()
        {
            InitializeComponent();
            f1 = this;
        }
        public DataTable dt = new DataTable();

        public static 新增医嘱处理 f1 = null;

        private void button_qx_Click(object sender, EventArgs e)
        {
            this.Hide();
            医嘱处理 c = new 医嘱处理();
            c.Show();
        }

        private void button_czyz_Click(object sender, EventArgs e)
        {
            string  sql="select max(WarnNo) from Warn where HosNo='"+this .tb_ZYNo .Text +"'";
           
            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {
                SqlCommand c = new SqlCommand(sql, conn);
                conn.Open();
                int n;
                if (dataGridView1.Rows.Count == 0)
                { n = Convert.ToInt32(c.ExecuteScalar()); }
                else
                {
                    int i = dataGridView1.Rows.Count;
                    n = Convert.ToInt32(dataGridView1 .Rows [i-2].Cells [1].Value ); }
          
            DataRow dr = null;
            dr = dt.NewRow();
            dr[0] = this.tb_ZYNo.Text;
            dr[1] = n + 1;
            dr[2] = this.cb_cl.Text;
            dr[3] = this.cb_lb.Text;
            dr[4] = this.dtp_kssj.Value;
            dr[5] = this.tb_nr.Text;
            dr[6] = this.tb_ypjl.Text;
            dr[7] = this.cb_tj.Text;
            dr[8] = this.tb_cs.Text;
            dr[9] = this.tb_zxsj.Text;
            dr[10] = this.dtp_tzsj.Value;
            dr[11] = "执行";
            dt.Rows.Add(dr);
            this.dataGridView1.DataSource = dt;
        } 
        }

        private void 新增医嘱处理_Load(object sender, EventArgs e)
        {
            DataColumn dc_zyh = new DataColumn("住院号", typeof(string));
            DataColumn dc_bh = new DataColumn("编号", typeof(int));
            DataColumn dc_cl = new DataColumn("长/临", typeof(string));
            DataColumn dc_lb = new DataColumn("类别", typeof(string));
            DataColumn dc_kssj = new DataColumn("开始时间", typeof(DateTime ));
            DataColumn dc_yznr = new DataColumn("医嘱内容", typeof(string));
            DataColumn dc_jl = new DataColumn("剂量", typeof(string));
            DataColumn dc_tj = new DataColumn("途径", typeof(string));
            DataColumn dc_cs = new DataColumn("次数", typeof(string));
            DataColumn dc_zxsj = new DataColumn("执行时间", typeof(string));
            DataColumn dc_tzsj= new DataColumn("停止时间", typeof(DateTime ));
            DataColumn dc_zt = new DataColumn("医嘱状态", typeof(string));
            dt.Columns.Add(dc_zyh);
            dt.Columns.Add(dc_bh);
            dt.Columns.Add(dc_cl); 
            dt.Columns.Add(dc_lb );
            dt.Columns.Add(dc_kssj);
            dt.Columns.Add(dc_yznr);
            dt.Columns.Add(dc_jl);
            dt.Columns.Add(dc_tj);
            dt.Columns.Add(dc_cs);
            dt.Columns.Add(dc_zxsj);
            dt.Columns.Add(dc_tzsj);
            dt.Columns.Add(dc_zt);
            
        }
        public static void add_w(DataTable tb)
        {
            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {
                SqlCommand com = new SqlCommand("insert into Warn(HosNo,WarnNo, WarNature,WarType,WarBegindate,WarContent,WarDosage,WarWay,WarTime,WarExecutedate,WarFinishdate,WarnCondition) values(@HosNo,@WarnNo, @WarNature,@WarType,@WarBegindate,@WarContent,@WarDosage,@WarWay,@WarTime,@WarExecutedate,@WarFinishdate,@WarnCondition)");
            conn .Open ();
                SqlDataAdapter da=new SqlDataAdapter ();
                SqlCommandBuilder cb=new SqlCommandBuilder (da);
                da.Update (tb );
                conn .Close ();

            }
        }

        private void button_bc_Click(object sender, EventArgs e)
        {
         
            
                for (int j = 0; j < dataGridView1.Rows.Count; j++)
                {
                    DataRow dr = dt.NewRow();

              
                for (int k = 0; k < dataGridView1.Columns.Count; k++)
                {
                    dr[k] = dataGridView1.Rows[j].Cells[k].Value.ToString();
                }
                dt.Rows.Add(dr );
                }

                add_w(dt );
            
        }

        }

    
}
